/*
 * Copyright 2010-2011 Mike Novak <michael.novakjr@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.codeversed.plugins.buzz.preference;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;

import com.codeversed.plugins.buzz.R;
import com.codeversed.plugins.buzz.oauth.BuzzDialog;
import com.codeversed.plugins.buzz.util.Constants;

public class BuzzPluginPreferences extends PreferenceActivity {
    private static final int DIALOG_OAUTH = 0x1;
    
    private Preference mOAuthPref;
    private SharedPreferences mPrefs;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        
        mPrefs = getSharedPreferences("buzz_oauth.prefs", MODE_PRIVATE);
        String token = mPrefs.getString(Constants.PREF_TOKEN, "");
        
        mOAuthPref = findPreference("oauth.login");
        mOAuthPref.setOnPreferenceClickListener(mOAuthPrefListener);
        
        if (!token.equals("")) {
            mOAuthPref.setTitle("Log out");
            mOAuthPref.setSummary("Press to log out of Google Buzz.");
        }
    }
    
    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        switch (id) {
        case DIALOG_OAUTH:
            BuzzDialog dialog = new BuzzDialog(this, mDialogListener);
            return dialog;
        default:
            throw new IllegalArgumentException("No valid dialog resource found to handle request: " + id);
        }
    }
    
    protected BuzzDialog.DialogListener mDialogListener = new BuzzDialog.DialogListener() {
        public void onComplete() {
            Log.d(Constants.LOG_TAG, "Dialog onComplete fired.");
            mOAuthPref.setTitle("Log out");
            mOAuthPref.setSummary("Press to log out of Google Buzz.");
        }
        
        public void onError() {
            Log.d(Constants.LOG_TAG, "Dialog onError fired.");
        }
        
        public void onCancel() {
            Log.d(Constants.LOG_TAG, "Dialog onCancel fired.");
        }
    };
    
    protected Preference.OnPreferenceClickListener mOAuthPrefListener = new Preference.OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            if (!mOAuthPref.getTitle().equals("Log out")) {
                showDialog(DIALOG_OAUTH);
            } else {
                mPrefs.edit().putString(Constants.PREF_TOKEN, "").putString(Constants.PREF_TOKEN_SECRET, "").commit();
                mOAuthPref.setTitle("Login");
                mOAuthPref.setSummary("Press to log into Google Buzz");
            }
            
            return true;
        }
    };
}
